package com.hnucm.cooklife.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.hnucm.cooklife.R;
import com.hnucm.cooklife.bean.db.Work;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MeWorkAdapter extends RecyclerView.Adapter<MeWorkAdapter.MeWorkHolder> {

    private Context mContext;
    private List<Work> mWorkList = new ArrayList<>();

    public MeWorkAdapter(Context context, List<Work> workList) {
        mContext = context;
        mWorkList.addAll(workList);
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setWorkList(List<Work> workList) {
        mWorkList.clear();
        mWorkList.addAll(workList);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MeWorkHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.work_item, parent, false);
        return new MeWorkHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MeWorkHolder holder, int position) {
        if(mWorkList != null){
            Work work = mWorkList.get(position);
            Glide.with(mContext)
                    .load(work.workPic)
                    .fitCenter()
                    .centerCrop()
                    .into(holder.mWorkPic);
        }
    }

    @Override
    public int getItemCount() {
        return mWorkList.size();
    }

    class MeWorkHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.workpic)
        ImageView mWorkPic;

        public MeWorkHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }
}
